﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Base;
using ERP.BusinessEntities;
using System.Data;
using Core;
using Utilities;

namespace MainApp.Modules.Shipment
{
    public class ShipmentEntity: BaseEntity
    {
        public SGVList<ICShipmentItemsInfo> lstICShipmentItems = new SGVList<ICShipmentItemsInfo>();
        public SGVList<ICShipmentItemsInfo> lstICShipmentItemConsolidates = new SGVList<ICShipmentItemsInfo>();
        public SGVList<ICShipmentItemDetailsInfo> lstICShipmentItemDetails = new SGVList<ICShipmentItemDetailsInfo>();

        public override void InitModuleObjects()
        {
            AddMainObject("ICShipments", new ICShipmentsInfo());
        }

        public override void InitLists()
        {
            base.InitLists();

            lstICShipmentItems = new SGVList<ICShipmentItemsInfo>();
            lstICShipmentItems.InitList(this, "ICShipments", "ICShipmentItems", "FK_ICShipments_ID");
            lstICShipmentItems.Binding("MainScreen", "ICShipmentItemsGridView");

            lstICShipmentItemConsolidates = new SGVList<ICShipmentItemsInfo>();
            lstICShipmentItemConsolidates.InitList(this, "", "ICShipmentItems", "");
            lstICShipmentItemConsolidates.Binding("ItemDetailScreen", "ICShipmentItemConolidatesGridView");

            lstICShipmentItemDetails = new SGVList<ICShipmentItemDetailsInfo>();
            lstICShipmentItemDetails.InitList(this, "ICShipments", "ICShipmentItemDetails", "FK_ICShipments_ID");
            lstICShipmentItemDetails.Binding("ItemDetailScreen", "ICShipmentItemDetailsGridView");
        }

        public override void BeforeSaveList()
        {
            base.BeforeSaveList();

            ICShipmentsInfo Shipment = (ICShipmentsInfo)MainObject;
            if (Shipment == null) return;

            DataSet dsShipmentItems = AppContext.ShipmentItemCtrl.GetAllObjectByColumnValue("FK_ICShipments_ID", Shipment.ID);
            if (dsShipmentItems != null && dsShipmentItems.Tables.Count > 0)
            {
                foreach (DataRow drShipmentItem in dsShipmentItems.Tables[0].Rows)
                {
                    ICShipmentItemsInfo ShipmentItem = (ICShipmentItemsInfo)AppContext.ShipmentItemCtrl.GetObjectFromDataRow(drShipmentItem);
                    if (ShipmentItem != null)
                        Inventory.UpdateInventory(InventoryDirection.In, InventoryType.OnHand, ShipmentItem.FK_ICProducts_ID, ShipmentItem.FK_ICStocks_ID,
                            ShipmentItem.BatchNo, ShipmentItem.LotNo, ShipmentItem.SerialNo, ShipmentItem.Qty);

                }
            }

        }

        public override void AfterSaveList()
        {
            base.AfterSaveList();

            ICShipmentsInfo Shipment = (ICShipmentsInfo)MainObject;
            if (Shipment == null) return;

            DataSet dsShipmentItems = AppContext.ShipmentItemCtrl.GetAllObjectByColumnValue("FK_ICShipments_ID", Shipment.ID);
            if (dsShipmentItems != null && dsShipmentItems.Tables.Count > 0)
            {
                foreach (DataRow drShipmentItem in dsShipmentItems.Tables[0].Rows)
                {
                    ICShipmentItemsInfo ShipmentItem = (ICShipmentItemsInfo)AppContext.ShipmentItemCtrl.GetObjectFromDataRow(drShipmentItem);
                    if (ShipmentItem != null)
                        Inventory.UpdateInventory(InventoryDirection.Out, InventoryType.OnHand, ShipmentItem.FK_ICProducts_ID, ShipmentItem.FK_ICStocks_ID,
                            ShipmentItem.BatchNo, ShipmentItem.LotNo, ShipmentItem.SerialNo, ShipmentItem.Qty);
                }
            }

        }
    }
}
